package 尚硅谷juc大厂.test21;

import lombok.extern.slf4j.Slf4j;
import org.openjdk.jol.info.ClassLayout;

@Slf4j
public class Test21 {

    /**
     * 4个线程去卖票
     */
    public  static void main(String[] args) {
        Ticket ticket=new Ticket();
        new Thread(()->{
            for (int i=0;i<55;i++){
                ticket.sale();
            }
        },"t1").start();

        new Thread(()->{
            for (int i=0;i<55;i++){
                ticket.sale();
            }
        },"t2").start();

        new Thread(()->{
            for (int i=0;i<55;i++){
                ticket.sale();
            }
        },"t3").start();

        new Thread(()->{
            for (int i=0;i<55;i++){
                ticket.sale();
            }
        },"t4").start();
    }
}

class Ticket{

    private int number=50;

    Object object=new Object();

    public void sale(){
        synchronized (object){
            if (number>0){
                System.out.println(Thread.currentThread().getName()+"\t"+"卖出第："+(number--));
            }
        }
    }
}
